-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================
hlb = require('libhlb')

function device_open()
	local ret = hlb.usb_open(0x04B4FFFF)
	if ret < 0 then
		print("Fail to open usb device")
		return false
	end
	
	return true
end

function device_close()
	local ret = hlb.usb_close()
	if ret < 0 then
		print("Fail to close usb device")
		return false
	end
	
	return true
end

function pause()
    io.write("\r\nAny key to continue ...")
    io.flush()
    io.read()
    print("")
end
-- -----------------------------------------------
function ctrl_5V(on)
    if type(on) ~= "number" then
        print("function param must be number type")
        return false
    end

    local msg = {0x40, 0xC5, on, 0, 0, 200}
    local ret
    ret = hlb.usb_control_msg(msg)

    if ret < 0 then
        print("ctrl_5V() ret = " .. ret)
        return false
    end

    return true
end
-- -----------------------------------------------
function send_ctrl_cmd(cmd_data)
    if type(cmd_data) ~= "table" then
        print("cmd_data must be table type")
        return false
    end

    local msg = {0x40, 0xb0, 0, 0, #cmd_data, 500}
    local ret

    if #cmd_data > 0 then
        ret = hlb.usb_control_msg(cmd_data, msg)
    else
        ret = hlb.usb_control_msg(msg)
    end
    
    if ret < 0 then
        print("send_ctrl_cmd() ret = " .. ret)
        return false
    end

    return true
end
-- -----------------------------------------------
ret = device_open()
if ret == false then
    print("device_open fail")
    return
end

ctrl_5V(1)
hlb.msdelay(100) --wait

cmd = {0x3F}
ret = send_ctrl_cmd(cmd)
if ret == false then
    print("send_ctrl_cmd() exec fail 0")
    return
end
print("Start FPGA App")

hlb.msdelay(500) --wait for reconfig

cmd = {0x00} --Reset
ret = send_ctrl_cmd(cmd)
if ret == false then
    print("send_ctrl_cmd() exec fail 1")
    return
end

cmd = {0x80, 0x00, 0x00, 0x00, 0x1f} -- start read adc data
ret = send_ctrl_cmd(cmd)
if ret == false then
    print("send_ctrl_cmd() exec fail 2")
    return
end
hlb.msdelay(100) --wait

count = 0
last_vol = 1000;
while count < 300000 do
    count = count + 1;
    -- bulk read test

    adc_data = hlb.usb_bulk_read(0x86, 1024, 1000)
    --print("adc_data types: " .. type(adc_data))
    if type(adc_data) == "number" then
        print(" ret = " .. adc_data)
        break
    end

    if type(adc_data) == "table" then
        --print("*** adc_data size: " .. #adc_data)
        local sum = 0
        local cnt = 0
        for i=1, #adc_data, 2 do
        --[[
    		io.write("0x" .. string.format("%02x,", adc_data[i]):upper())
            io.write("0x" .. string.format("%02x,", adc_data[i+1]):upper())
    		if ((i-1)%128) == 0 then
    			print("")
    		end
        --]]
            for j=0, adc_data[i+1] do
                sum = sum +adc_data[i]
                cnt = cnt + 1
                --print("adc_data[i+1] = ", adc_data[i+1])
            end
        end
        --print("\r\n*** sum = " .. sum, ", cnt = " .. cnt)

        local average = math.floor(sum / cnt)
        local vol = ((average - 131) * 0.004) * 1051 / 51
        if last_vol ~= vol then
            last_vol = vol
            print("*** vol = " .. vol .. ", average = " .. average .. ", compression ratio = " .. #adc_data * 100/cnt .. "%")
        end

	end
    --hlb.msdelay(10)
end
---[[
cmd = {0x00} --Reset
ret = send_ctrl_cmd(cmd)
if ret == false then
    print("send_ctrl_cmd() exec fail 1")
    return
end
--]]
ctrl_5V(0)

device_close()
